/**
 * @param {string[]} words
 * @return {number}
 */
 var longestPalindrome = function(words) {
  const map = new Map()
  const re = new Map()
  let ans = 0
  for(let w of words){
    if(w[0] === w[1]){
      re.set(w, (re.get(w) || 0) + 1)
      continue
    }
    let c1 = w[0], c2 = w[1]
    let need = c2 + c1
    if(map.has(need) && map.get(need) > 0){
      ans += 4
      map.set(need, map.get(need)-1)
    }else{
      map.set(w, (map.get(w) || 0) + 1)
    }
  }
  const vals = [...re.values()].sort((a,b) => b-a)
  let flag = true
  for(let x of vals){
    if(x % 2 !== 0 && flag){
      ans += x*2
      flag = false
    }else{
      if(x % 2) ans += (x-1)*2
      else ans += x * 2
    }
  }
  return ans
};